﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.App
{
    public class WebApplicationBuilder
    {
        public IWebHostEnvironment Environment { get; }

        public ConfigurationManager Configuration { get; }

        public IServiceCollection Services { get; }

        public ILoggingBuilder Logging { get; set; }

        public WebApplicationBuilder(
            ILoggingBuilder logging,
            IServiceCollection services,
            IWebHostEnvironment environment,
            ConfigurationManager configurationManager)
        {
            Services = services;
            Environment = environment;
            Configuration = configurationManager;
        }

        public static WebApplicationBuilder Create(string[] args)
        {
            var configurationManager = new ConfigurationManager();
            configurationManager.AddCommandLine(args);
            configurationManager.AddEnvironmentVariables("ASPNETCORE_");
            configurationManager.SetBasePath(Directory.GetCurrentDirectory());
            configurationManager.AddJsonFile("appsettings.json");
            var webHostEnvironment = new WebHostEnvironment(configurationManager["Environment"]);
            
            var services = new ServiceCollection();
            var logging = new LoggingBuilder(services);

            return new WebApplicationBuilder(
                logging,
                services,
                webHostEnvironment,
                configurationManager);
        }

        class LoggingBuilder : ILoggingBuilder
        {
            public IServiceCollection Services { get; }

            public LoggingBuilder(IServiceCollection services)
            {
                Services = services;
            }
        }
    }
}
